package com.ruoyi.project.pro.manager.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.cms.CmsNotDeleteException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.CmsTypeVo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.pro.classes.domain.ProClasses;
import com.ruoyi.project.pro.classes.domain.ProClassesVo;
import com.ruoyi.project.pro.classes.service.IClassesService;
import com.ruoyi.project.pro.line.domain.ProLine;
import com.ruoyi.project.pro.line.domain.ProLineVo;
import com.ruoyi.project.pro.line.service.ILineService;
import com.ruoyi.project.pro.manager.domain.ProManager;
import com.ruoyi.project.pro.manager.domain.ProManagerVo;
import com.ruoyi.project.pro.manager.service.IProManagerService;
import com.ruoyi.project.pro.picture.domain.ProPicture;
import com.ruoyi.project.pro.picture.service.IProPictureService;
import com.ruoyi.project.pro.size.domain.ProSize;
import com.ruoyi.project.pro.size.domain.ProSizeVo;
import com.ruoyi.project.pro.size.service.ISizeService;
import com.ruoyi.project.pro.style.domain.ProStyle;
import com.ruoyi.project.pro.style.domain.ProStyleVo;
import com.ruoyi.project.pro.style.service.IStyleService;
import com.ruoyi.project.pro.texture.domain.ProTexture;
import com.ruoyi.project.pro.texture.domain.ProTextureVo;
import com.ruoyi.project.pro.texture.service.ITextureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-17 16:33
 * @description: 产品管理的控制层
 **/
@Controller
@RequestMapping("/pro/manager")
public class ProManagerController extends BaseController {
    @Autowired
    private IProManagerService proManagerService;
    @Autowired
    private ILineService lineService;
    @Autowired
    private ISizeService sizeService;
    @Autowired
    private IStyleService styleService;


    @Autowired
    private ITextureService textureService;

    @Autowired
    private IClassesService classesService;
    @Autowired
    private IProPictureService proPictureService;

    private String prefix = "pro/manager";

    /**
     * 跳转到产品管理的html页面
     * @return 添加的页面地址
     */
    @RequiresPermissions("pro:manager:view")
    @GetMapping()
    public String manager() {
        return prefix + "/manager";
    }
    /**
     * 跳转到收藏夹产品管理的html页面
     * @return
     */
    @RequiresPermissions("pro:manager:perFav")
    @GetMapping("/perFav")
    public String perFav() {
        return prefix + "/perFav";
    }
    //PerFav
    /**
     * 分页查询收藏夹产品
     * @param proManager
     * @return
     */
    @RequiresPermissions("pro:manager:PerFav")
    @PostMapping("/viewPerFav")
    @ResponseBody
    public TableDataInfo PerFav() {
        startPage();
        List<ProManagerVo> proManagerVos = proManagerService.selectManagerFavList();
        return getDataTable(proManagerVos);
    }


    /**
     * 跳转到修改产品的html页面,在跳转之前先回显数据
     * @param modelMap 共享数据
     * @return 修改的页面地址
     */

    @GetMapping("/edit/{managerId}")
    public String edit(@PathVariable Long managerId, ModelMap modelMap) {

        //根据传递的id查询产品数据,并将数据放到作用域中
         ProManager proManager=  proManagerService.selectProById(managerId);
        Long pictureId = proManager.getPictureId();


         modelMap.addAttribute("proManager",proManager);
        ProVo(modelMap, pictureId);

        return prefix + "/edit";
    }

    private void ProVo(ModelMap modelMap, Long pictureId) {
        ProLine proLine = new ProLine();
        ProSize proSize = new ProSize();
        ProStyle proStyle = new ProStyle();
        ProTexture proTexture = new ProTexture();
        ProClasses proClasses = new ProClasses();
        ProPicture proPicture = new ProPicture();


        List<ProClasses> proClassess = classesService.selectClasses(proClasses);
        List<ProSize> proSizes = sizeService.selectSize(proSize);
        List<ProLine> proLines = lineService.selectLine(proLine);
        List<ProStyle> proStyles = styleService.selectStyle(proStyle);
        List<ProTexture> proTextures = textureService.selectTexture(proTexture);

        List<ProStyleVo> proStyleVos = new ArrayList<>();
        List<ProLineVo> proLineVos = new ArrayList<>();
        List<ProSizeVo> proSizeVos = new ArrayList<>();
        List<ProClassesVo> proClassesVos = new ArrayList<>();
        List<ProTextureVo> proTextureVos = new ArrayList<>();
        for (ProTexture texture : proTextures) {
            ProTextureVo proTextureVo = new ProTextureVo();
            BeanUtils.copyBeanProp(proTextureVo, texture);
            proTextureVos.add(proTextureVo);

        }
        for (ProClasses proClasse : proClassess) {
            ProClassesVo proClassesVo = new ProClassesVo();
            BeanUtils.copyBeanProp(proClassesVo, proClasse);
            proClassesVos.add(proClassesVo);

        }
        for (ProStyle style : proStyles) {
            ProStyleVo proStyleVo = new ProStyleVo();
            BeanUtils.copyBeanProp(proStyleVo, style);
            proStyleVos.add(proStyleVo);

        }
        for (ProSize size : proSizes) {
            ProSizeVo proSizeVo = new ProSizeVo();
            BeanUtils.copyBeanProp(proSizeVo, size);
            proSizeVos.add(proSizeVo);

        }
        for (ProLine line : proLines) {
            ProLineVo proLineVo = new ProLineVo();
            BeanUtils.copyBeanProp(proLineVo, line);
            proLineVos.add(proLineVo);
        }
        modelMap.addAttribute("proLineVos", proLineVos);
        modelMap.addAttribute("proSizeVos", proSizeVos);
        modelMap.addAttribute("proStyleVos", proStyleVos);
        modelMap.addAttribute("proTextureVos", proTextureVos);
        modelMap.addAttribute("proClassesVos", proClassesVos);
        ProPicture picture = proPictureService.selectPictureById(pictureId);
        String picturePath = picture.getPicturePath();

        modelMap.addAttribute("picturePath", picturePath);
    }

    /**
     * 跳转到添加的页面,在跳转前先加载数据
     * @return
     */
    @RequiresPermissions("pro:manager:add")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        ProLine proLine=new ProLine();
        ProSize proSize=new ProSize();
        ProStyle proStyle=new ProStyle();
        ProTexture proTexture=new ProTexture();
        ProClasses proClasses=new ProClasses();
        List<ProClasses> proClassess = classesService.selectClasses(proClasses);
        List<ProSize> proSizes = sizeService.selectSize(proSize);
        List<ProLine> proLines = lineService.selectLine(proLine);
        List<ProStyle> proStyles = styleService.selectStyle(proStyle);
        List<ProTexture> proTextures = textureService.selectTexture(proTexture);
        List<ProStyleVo> proStyleVos=new ArrayList<>();
        List<ProLineVo> proLineVos=new ArrayList<>();
        List<ProSizeVo> proSizeVos=new ArrayList<>();
        List<ProClassesVo> proClassesVos=new ArrayList<>();
        List<ProTextureVo> proTextureVos=new ArrayList<>();
        for (ProTexture texture : proTextures) {
            ProTextureVo proTextureVo=new ProTextureVo();
            BeanUtils.copyBeanProp(proTextureVo,texture);
            proTextureVos.add(proTextureVo);

        }
        for (ProClasses proClasse : proClassess) {
            ProClassesVo proClassesVo=new ProClassesVo();
            BeanUtils.copyBeanProp(proClassesVo,proClasse);
           proClassesVos.add(proClassesVo);

        }
        for (ProStyle style : proStyles) {
            ProStyleVo proStyleVo=new ProStyleVo();
            BeanUtils.copyBeanProp(proStyleVo,style);
            proStyleVos.add(proStyleVo);

        }
        for (ProSize size : proSizes) {
            ProSizeVo proSizeVo=new ProSizeVo();
            BeanUtils.copyBeanProp(proSizeVo,size);
            proSizeVos.add(proSizeVo);

        }
        for (ProLine line : proLines) {
           ProLineVo proLineVo=new ProLineVo();
            BeanUtils.copyBeanProp(proLineVo,line);
            proLineVos.add(proLineVo);
        }
        modelMap.addAttribute("proLineVos",proLineVos);
        modelMap.addAttribute("proSizeVos",proSizeVos);
        modelMap.addAttribute("proStyleVos",proStyleVos);
        modelMap.addAttribute("proTextureVos",proTextureVos);
        modelMap.addAttribute("proClassesVos",proClassesVos);

        return prefix + "/add";
    }



    /**
     * 分页查询产品
     * @param proManager
     * @return
     */
    @RequiresPermissions("pro:manager:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProManager proManager) {
        startPage();
        List<ProManagerVo> proManagerVos = proManagerService.selectManagerList(proManager);
        return getDataTable(proManagerVos);
    }


    /**
     * 添加产品
     * @param proManager 添加产品
     * @return 添加结果
     */

    @RequiresPermissions("pro:manager:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ProManager proManager){
       return toAjax(proManagerService.insertPro(proManager));

    }

    /**
     * 修改的操作
     * @param proManager
     * @return
     */
    @RequiresPermissions("pro:manager:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ProManager proManager)
    {
        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(proManagerService.checkManagerNameUnique(proManager)))
        {
            return error("修改产品品名'" + proManager.getManagerName() + "'失败，品名已存在");
        }

        return toAjax(proManagerService.updatePro(proManager));
    }

    /**
     * 删除产品的操作
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("pro:manager:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {

        try {
            int result = proManagerService.removeProById(ids);
            return toAjax(result);
        } catch (CmsNotDeleteException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
  //  removeFav

    /**
     * 跳转到产品详情展示页面
     * @param modelMap 共享数据
     * @return 修改的页面地址
     */

    @GetMapping("/view")

    public String view(String managerId, ModelMap modelMap) {

        //根据传递的id查询产品数据,并将数据放到作用域中
        ProManager proManager=  proManagerService.selectProById(Long.valueOf(managerId));
        Long pictureId = proManager.getPictureId();

        modelMap.addAttribute("proManager",proManager);
        ProVo(modelMap, pictureId);

        return prefix + "/view";
    }

}
